package vno.fux.res.manager
{
	import flash.events.EventDispatcher;
	
	import vno.fux.GlobalVar;
	import vno.fux.res.pool.MapResource;
	import vno.fux.res.pool.RM_MRM;
	import vno.fux.res.pool.ResourcePool;

	internal class MapResourceManager extends EventDispatcher
	{
		private static var instance:MapResourceManager;
		private const obj_id:String = "RM_MRM";
		private var _resourcePool:ResourcePool;
		private var _loaderManager:LoaderManager;
		private var _resInfo:XML;
		/*
		var mapRes:XML=<mapResInfo/>;
		for(var i:int =0;resourse.mapRes.map[i]!=null;i++)
		{
		mapRes.appendChild(<map/>);
		mapRes.map[i]=resourse.mapRes.map[i];
		}
		return 	mapRes;
		*/
		
		public function MapResourceManager(pvt:PrivateClass, resInfo_:XML)
		{
			_resourcePool = ResourcePool.getInstance();
			_loaderManager =  ResourceManager.loader;
			_resInfo = resInfo_;
		}
		
		public function loadThumbMap(id:uint):void
		{
			//trace(_resInfo.map.(@id==id).thumbmap[0].@url);
			var url:String = _resInfo.map.(@id==id).thumbmap[0].@url+"thumbMap.jpg";
			var mapRes:MapResource = _resourcePool.mapRes.getRes(id) as MapResource;
			var mission:ResourceMission = new ResourceMission(GlobalVar.webUrl+url, mapRes.RM_MRM::_thumbMap, CommonLoader.PIC, mapRes, _loaderManager.getId());
			_loaderManager.addMission(mission);
		}
		
		public function loadMapTile(params:Object):void
		{
			var id:uint = params["id"];
			var x:uint = params["x"];
			var y:uint = params["y"];
			var url:String = _resInfo.map.(@id==id).tiles[0].@url +"x"+x+"y"+y+"z0.jpg";
			var mapRes:MapResource = _resourcePool.mapRes.getRes(id) as MapResource;
			var mission:ResourceMission = new ResourceMission(GlobalVar.webUrl+url, mapRes.RM_MRM::_mapTileMatrix[x][y], CommonLoader.PIC, mapRes, _loaderManager.getId());
			_loaderManager.addMission(mission);
		}
		
		public static function init(resInfo_:XML):void
		{
			if(MapResourceManager.instance)
			{
				throw new Error("You can't init the MapResourceManager twice!")
			}
			MapResourceManager.instance = new MapResourceManager(new PrivateClass, resInfo_);
		}
		
		public static function getInstance():MapResourceManager
		{
			return MapResourceManager.instance;
		}
	}
}
class PrivateClass
{
	public function PrivateClass()
	{
	}
}